/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package BLL;

import DAL.DALOrder;
import DTO.DTOInventory;
import DTO.DTOOrder;
import DTO.DTOOrderDetail;
import DTO.DTOProduct;
import java.util.ArrayList;
import java.util.Date;

/**
 *
 * @author Pham
 */
public class BLLOrder {
    DALOrder dalOrd;
    BLLProduct bllPro;
    BLLOrderDetail bllOrdDetail;
    BLLInventory bllInv;
    
    public BLLOrder()
    {
        dalOrd = new DALOrder();
        bllPro = new BLLProduct();
        bllOrdDetail = new BLLOrderDetail();
        bllInv = new BLLInventory();
    }
    
    //method get all orders
    public ArrayList<DTOOrder> getOrders()
    {
        return dalOrd.getOrders();
    }
    
    //method get the order by id
    public DTOOrder getOrderbyId(String Ordid)
    {
        return dalOrd.getOrderbyId(Ordid);
    }
    
    //method filter order
    public ArrayList<DTOOrder> filterOrder(Date dateFrom, Date dateTo, int sate)
    {
        return dalOrd.filterOrder(dateFrom, dateTo, sate);
    }
    
    //method execute export
    public boolean exeExport(DTOOrder dtoOrd){
        try{
            if(checkOrderAvaiable(dtoOrd))
            {
                Date currentDateTime = new Date();
                dtoOrd.setDateExport(currentDateTime);
                dtoOrd.setOrderState(2);
                if(dalOrd.updateState(dtoOrd))
                {
                    
                    int count = 0;
                    ArrayList<DTOOrderDetail> dtoOrdDetail = bllOrdDetail.getOrdDetailbyID(dtoOrd.getOrderId());
                    for(DTOOrderDetail ItemdtoOrdDetail : dtoOrdDetail)
                    {
                        DTOInventory dtoInv = new DTOInventory();
                        DTOInventory dtoCurrentInv = bllInv.getCurrentInv(ItemdtoOrdDetail.getDtoPro().getProductID());
                            int newQuantity = dtoCurrentInv.getInventoryQuatity() - ItemdtoOrdDetail.getQuantity();
                            dtoInv.setInventoryQuatity(newQuantity);
                            dtoInv.setInventoryDate(currentDateTime);
                            dtoInv.setDtoPro(ItemdtoOrdDetail.getDtoPro());
                            if(bllInv.insertNewInventory(dtoInv)){
                                DTOProduct dtoPro = dtoInv.getDtoPro();
                                dtoPro.setProductQuantity(newQuantity);
                                if(bllPro.updateRowProduct(dtoPro))
                                    count ++;
                            }
                    }
                    if(dtoOrdDetail.size() == count)
                        return true;
                    else
                        return false;
                
                }
                else
                    return false;
            }
            else
                return false;
        }
        catch(Exception ex){
            System.err.printf(ex.getMessage());
            return false;
        }
    }
    
    //method check avaiable of order
    public boolean checkOrderAvaiable(DTOOrder dtoOrd)
    {
        if(dtoOrd.getOrderState() != 2){
            for(DTOOrderDetail dtoOrdDetail : bllOrdDetail.getOrdDetailbyID(dtoOrd.getOrderId()))
            {
                DTOInventory dtoCurrentInv = bllInv.getCurrentInv(dtoOrdDetail.getDtoPro().getProductID());
                if(dtoCurrentInv != null){
                    if(dtoCurrentInv.getInventoryQuatity() < dtoOrdDetail.getQuantity())
                        return false;
                }
                else
                    return false;
            }
        }
        else 
            return false;
        
        return true;
    }

    //update state of the order
    public boolean updateState(DTOOrder dtoOrd) {
        return dalOrd.updateState(dtoOrd);
    }
    
    //method create new order
    public boolean createOrder(DTOOrder dtoOrd)
    {
        return dalOrd.createOder(dtoOrd);
    }
    
    //method delete order
    public boolean deleteOrder(String orderId)
    {
        return dalOrd.deleteOrder(orderId);
    }
}
